package blackeagle.ems.dal;

import se.oru.informatik.StringHelper;

/**
 * Represents a WHERE-statement in a DML-query.
 */
public class Where {
    private ComparisonMethod comparisonMethod;
    private String fieldName;
    private Object value;
    
    /**
     * Creates a new instance of the Where-class using 'Equals' as its comparison method.
     * @param fieldName The column to check against.
     * @param value The value to find.
     * 
     * @see blackeagle.ems.dal.ComparisonMethod
     */
    public Where(String fieldName, Object value){
        this(fieldName, ComparisonMethod.Equals, value);
    }
    
    /**
     * Creates a new instance of the Where-class.
     * @param fieldName The column to check against.
     * @param comparisonMethod The comparison method to use.
     * @param value The value to find.
     */
    public Where(String fieldName, ComparisonMethod comparisonMethod, Object value){
        this.fieldName=fieldName;
        this.comparisonMethod = comparisonMethod;
        this.value=value;
    }
    
    /**
     * Creates a WHERE-statement based on the given column name, comparison method and column value. The WHERE-keyword is omitted.
     * @return A <code>String</code> value containing the literal representation of the WHERE-statement.
     */
    @Override
    public String toString(){
        String c = "";
        
        switch(this.comparisonMethod){
            default:
            case Equals:
                c="=";
                break;
            case NotEquals:
                c="!=";
                break;
            case Like:
                c="LIKE";
                break;
            case NotLike:
                c="NOT LIKE";
                break;
        }
        
        return StringHelper.sprintf("# # '#'", this.fieldName, c, this.value);
    }
}
